import datasource from "@/views/datasource/api/source.js"
import { ref } from "vue"

// 数据源列表
export const dataSourceList = ref({})
export const useDataCube = ref({})
export const dimension = ref({})
export const useDatasourceList = async () => {
  const loading = ref(false)
  const res = await datasource.getPageList()
  dataSourceList.value = res.data.data
  loading.value = false
  return {
    loading,
    dataSourceList
  }
}
useDatasourceList()
// 数据表
export const sourceTable = ref([])
export const useSourceTable = () => {
  const loading = ref(false)
  let oldSourceTable = []
  const visible = ref(false)
  const run = (id) => {
    loading.value = true
    datasourceTable(id).then((res) => {
      sourceTable.value = res.data
      oldSourceTable = JSON.parse(JSON.stringify(res.data))
      visible.value = !visible.value
      loading.value = false
    })
  }
  const sourceColumns = [
    {
      title: "表名",
      field: "table_name",
      sortable: true
    },
    {
      title: "表类型",
      field: "table_type",
      slots: { default: "file" }
    },
    {
      title: "数据行数",
      field: "table_rows",
      sortable: true
    },
    {
      title: "数据大小",
      field: "table_size"
    },
    {
      title: "创建时间",
      field: "create_time",
      sortable: true
    },
    {
      title: "更新时间",
      field: "update_time",
      sortable: true
    },
    {
      title: "表注释",
      field: "table_comment"
    },
    {
      title: "操作",
      width: 250,
      slots: { default: "action" }
    }
  ]

  const sourceColumn = ref({
    column: [
      { field: "name", title: "字段名称" },
      { field: "type", title: "字段类型" },
      { field: "comment", title: "字段注释" }
    ],
    data: []
  })

  // 预览数据
  const previewColumns = ref({
    columns: [],
    data: [],
    total: 0
  })
  const sourcePreview = (data) => {
    datasourcePreview(data).then((res) => {
      previewColumns.value.columns = res.data.data.length
        ? Object.keys(res?.data?.data[0]).map((item) => {
            return {
              field: item,
              title: item
            }
          })
        : []
      previewColumns.value.data = res.data.data
      previewColumns.value.total = res.data.total
    })
  }

  // 搜索
  const onChangeSearch = (keyword) => {
    const filterVal = String(keyword).trim().toLowerCase()
    if (filterVal) {
      const filterRE = new RegExp(filterVal, "gi")
      const searchProps = ["table_name", "table_comment"]
      const rest = oldSourceTable.filter((item) =>
        searchProps.some((key) => String(item[key]).toLowerCase().indexOf(filterVal) > -1)
      )
      sourceTable.value = rest.map((row) => {
        const item = Object.assign({}, row)
        searchProps.forEach((key) => {
          item[key] = String(item[key]).replace(filterRE, (match) => `${match}`)
        })
        return item
      })
    } else {
      sourceTable.value = oldSourceTable
    }
  }
  return {
    loading,
    run,
    sourceColumns,
    visible,
    sourceTable,
    sourceColumn,
    sourcePreview,
    previewColumns,
    onChangeSearch
  }
}
